package com.example.fileserver_API.Interfaces.Service.WorkBench;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.fileserver_API.Dao.SysappsettingMappers;
import com.example.fileserver_API.Entity.Dto.PageParmar.PageParmar;
import com.example.fileserver_API.Entity.PO.sysappsettings;
import com.example.fileserver_API.Entity.PO.uploadpictures;
import com.example.fileserver_API.Entity.VO.APIResult;
import com.example.fileserver_API.FixedValue.SetSettings;
import com.example.fileserver_API.Interfaces.IService.WorkBench.ISysSettingService;
import com.example.fileserver_API.utils.APIResultUtils;
import com.example.fileserver_API.utils.GetGuid;
import com.example.fileserver_API.utils.RedisUtils;
import com.example.fileserver_API.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SysSettingService implements ISysSettingService {
    @Resource
    SysappsettingMappers sysappsettingMappers;
    @Resource
    RedisUtils redisUtils;

    /**
     * 分页获取策略
     * @param pageParmar 分页
     * @return APIResult
     */
    @Override
    public APIResult GetSysSetting(PageParmar pageParmar) {
        if (pageParmar==null)
            pageParmar = new PageParmar();
        String guid = "";// 唯一标识符
        String appkey = "";// 设置的key
        String appvalue = "";// 设置的值
        Object argsObject = pageParmar.getArgs();
        if (argsObject instanceof Map) {
            Map<String, Object> argsMap = (Map<String, Object>) argsObject;
            // 假设 "typename" 的值是字符串类型
            guid = (String) argsMap.getOrDefault("guid", "");
            appkey = (String) argsMap.getOrDefault("appkey", "");
            appvalue = (String) argsMap.getOrDefault("appvalue", "");
        }
        QueryWrapper<sysappsettings> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(guid))
            queryWrapper.eq("guid",guid);
        if (StringUtils.isNotBlank(appkey))
            queryWrapper.eq("appkey",appkey);
        if (StringUtils.isNotBlank(appvalue))
            queryWrapper.eq("appvalue",appvalue);
        IPage<sysappsettings> iPage = new Page<>(pageParmar.getPageNo(),pageParmar.getPageSize());
        IPage<sysappsettings> data = sysappsettingMappers.selectPage(iPage,queryWrapper);
        return APIResultUtils.success("",data);
    }

    /**
     * 添加一个策略
     * @param sysappsettings 策略内容
     * @return APIResult
     */
    @Override
    public APIResult AddSysSetting(sysappsettings sysappsettings) {
        if (sysappsettings==null)
            return APIResultUtils.error("请填写内容！");
        if (StringUtils.isBlank(sysappsettings.getAppkey()))
            return APIResultUtils.error("请填写策略Key");
        if (StringUtils.isBlank(sysappsettings.getAppvalue()))
            return APIResultUtils.error("请填写策略值");
        if (StringUtils.isBlank(sysappsettings.getAppcontent()))
            return APIResultUtils.error("请填写策略描述");
        sysappsettings.setGuid(GetGuid.ToString());
        sysappsettings.setCreatetime(new Date());
        boolean issave = redisUtils.SetHashValue(SetSettings.sys_syssetting,sysappsettings.getAppkey(),sysappsettings.getAppvalue());
        if (issave){
            int successcount = sysappsettingMappers.insert(sysappsettings);
            return successcount>0?APIResultUtils.success():APIResultUtils.error("添加失败");
        }
        return APIResultUtils.error("添加失败");
    }

    /**
     * 修改一个策略
     * @param sysappsettings 策略内容
     * @return APIResult
     */
    @Override
    public APIResult UpdateSysSetting(sysappsettings sysappsettings) {
        if (sysappsettings==null)
            return APIResultUtils.error("请填写内容！");
        if (StringUtils.isBlank(sysappsettings.getAppkey()))
            return APIResultUtils.error("请填写策略Key");
        if (StringUtils.isBlank(sysappsettings.getAppvalue()))
            return APIResultUtils.error("请填写策略值");
        if (StringUtils.isBlank(sysappsettings.getAppcontent()))
            return APIResultUtils.error("请填写策略描述");
        sysappsettings.setCreatetime(new Date());
        UpdateWrapper<sysappsettings> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("guid",sysappsettings.getGuid());
        updateWrapper.set("appkey",sysappsettings.getAppkey());
        updateWrapper.set("appvalue",sysappsettings.getAppvalue());
        updateWrapper.set("appcontent",sysappsettings.getAppcontent());
        updateWrapper.set("createtime",new Date());
        boolean issave = redisUtils.SetHashValue(SetSettings.sys_syssetting,sysappsettings.getAppkey(),sysappsettings.getAppvalue());
        if (issave){
            int successcount = sysappsettingMappers.update(null,updateWrapper);
            return successcount>0?APIResultUtils.success():APIResultUtils.error("修改失败");
        }
        return APIResultUtils.error("修改失败");
    }

    /**
     * 删除策略
     * @param guid 策略编号
     * @return APIResult
     */
    @Override
    public APIResult DeleteSysSetting(String guid) {
        if (guid.isEmpty())
            return APIResultUtils.error("请选择一些策略进行删除！");
        QueryWrapper<sysappsettings> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("guid",guid);
        sysappsettings data = sysappsettingMappers.selectOne(queryWrapper);
        boolean issave = redisUtils.DeleteHashKey(SetSettings.sys_syssetting,data.getAppkey());
        if (issave){
            int successcount = sysappsettingMappers.delete(queryWrapper);
            return successcount>0?APIResultUtils.success():APIResultUtils.error("删除失败");
        }
        return APIResultUtils.error("删除失败");
    }
}
